package cn.manjuu.touhou.engine;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import cn.manjuu.touhou.R;
import cn.manjuu.touhou.activity.DramaActivity;
import cn.manjuu.touhou.activity.MainActivity;
import cn.manjuu.touhou.activity.fragment.CardInfoFragment;
import cn.manjuu.touhou.activity.fragment.DiTuFragment;
import cn.manjuu.touhou.activity.fragment.MakeMoneyFragment;
import cn.manjuu.touhou.activity.fragment.ToyMachineFragment;

public class NavigationEngine {

	private FragmentTransaction mTransaction;
	private FragmentManager mFragmentManager;
	private static NavigationEngine mInstance;
	private MainActivity mActivity;
	private ToyMachineFragment toyMachineFragment;
	private CardInfoFragment cardInfoFragment;
	private MakeMoneyFragment makeMoneyFragment;
	private DiTuFragment diTuFragment;

	public void setmActivity(MainActivity mActivity) {
		this.mActivity = mActivity;
	}

	public static NavigationEngine getInstance() {
		if (null == mInstance) {
			mInstance = new NavigationEngine();
		}
		return mInstance;
	}

	private NavigationEngine() {
		super();
	}

	public void init() {
		// TODO Auto-generated method stub
		mFragmentManager = mActivity.getSupportFragmentManager();

		mTransaction = mFragmentManager.beginTransaction();
		toyMachineFragment = new ToyMachineFragment();
		mTransaction.replace(R.id.fl_main_fragment, toyMachineFragment);
		mTransaction.commit();
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_toy_machine:
			toToyMachineFragment();
			break;
		case R.id.btn_card_info:
			toCardInfoFragment();
			break;
		case R.id.btn_make_money:
			toMakeMoneyFragment();
			break;
		case R.id.btn_ditu:
			toDiTuFragment();
			break;
		}

	}

	private void toDiTuFragment() {
		// TODO Auto-generated method stub
		mTransaction = mFragmentManager.beginTransaction();
		if (null == diTuFragment) {
			diTuFragment = new DiTuFragment();
		}
		mTransaction.replace(R.id.fl_main_fragment, diTuFragment);
		mTransaction.commit();
	}

	public void toMakeMoneyFragment() {
		mTransaction = mFragmentManager.beginTransaction();
		if (null == makeMoneyFragment) {
			makeMoneyFragment = new MakeMoneyFragment();
		}
		mTransaction.replace(R.id.fl_main_fragment, makeMoneyFragment);
		mTransaction.commit();
	}

	public void toCardInfoFragment() {
		mTransaction = mFragmentManager.beginTransaction();
		if (null == cardInfoFragment) {
			cardInfoFragment = new CardInfoFragment();
		}
		mTransaction.replace(R.id.fl_main_fragment, cardInfoFragment);
		mTransaction.commit();
	}

	public void toToyMachineFragment() {
		// TODO Auto-generated method stub
		mTransaction = mFragmentManager.beginTransaction();
		if (null == toyMachineFragment) {
			toyMachineFragment = new ToyMachineFragment();
		}
		mTransaction.replace(R.id.fl_main_fragment, toyMachineFragment);
		mTransaction.commit();
	}
	
	public void toDaramActivity(String theme) {
		Intent intent = new Intent(mActivity, DramaActivity.class);
		intent.putExtra("theme", theme);
		mActivity.startActivity(intent);
		mActivity.overridePendingTransition(R.anim.alpha_enter, R.anim.alpha_exit);
	}
}
